/*
 * @ author madman 
 * @ use 号码管理
 */
const CountryModel = require('../../models/nexmo/country.model');
const PriceModel = require('../../models/nexmo/price.model');
const ClientModel = require('../../models/nexmo/client.model');
const UsersetupModel = require('../../models/nexmo/usersetup.model');
const Utils = require('../utils');
const UtilsNexmo = require('../utils/nexmo');

class Numbers {
    // 购买号码
    async buy(ctx) {
        const _body = ctx.request.body
        var res = await UtilsNexmo.numberBuy({
            country:_body.country,
            msisdn: _body.msisdn
        })
        if(res) {
            const _params = Utils._params(_body)
            const Country = await PriceModel.findOne({countryCode: _params.country}, {_id: 1})
            const UserList = await UsersetupModel.find({lock: '1'})
            _params.nextBillingDate =  Utils.currDateTo30();
            _params.user = UserList[0].name
            //查询国家列表里的城市ID
            if (Country) {
                _params.country= Country._id
            }
            var isMsisdn = await ClientModel.findOne({msisdn: _body.msisdn});
            if(isMsisdn) {
                ctx.error({ msg: '购买失败，之前购买过，请删除后再购买！' }); 
            } else {
                _params.nextBillingDay =  30; 
                var dataRes = await ClientModel.create(_params);
                if (dataRes) {
                    await UsersetupModel.updateOne({_id: UserList[0]._id}, {$set: { lock: '0'}})
                    await UtilsNexmo.setupNumber({ country:_body.country, msisdn: _body.msisdn }) // LINK号码
                    ctx.success({ msg: '购买成功' }); 
                }
            }
        } 
    }
    //获取号码列表
    async list(ctx) {
        var res =  await UtilsNexmo.numberSearch(ctx.query)
        const Price = await PriceModel.find({});
        if (!res.numbers) {
            return ctx.success({
                data:{
                    total: 0,
                    pages: 1,
                    result: []
                }
            });
        } else {
            const _number = res.numbers.map((item) => {
                Price.map((priceitem) => {
                    if (item.country === priceitem.countryCode) {
                        item.inPrice = priceitem.inPrice
                        item.outPriceCost = priceitem.outPriceCost
                        item.dialingPrefix = priceitem.dialingPrefix
                        item.monthPrice = priceitem.monthPrice
                        item.inPriceCost = priceitem.inPriceCost
                        item.currencyName = priceitem.countryName
                        item.telOutPrice = priceitem.telOutPrice
                        item.mobileOutPrice = priceitem.mobileOutPrice
                    }
                })
                return item
            })
            return ctx.success({
                data:{
                    total: res.count,
                    pages: Math.ceil(res.count / 10),
                    result: _number
                }    
            });
        }
    }
}

export default new Numbers;